/*-----------------------------------------------------------------------------
	The contents of this file are subject to the Mozilla Public License
	Version 1.1 (the "License"); you may not use this file except in
	compliance with the License. You may obtain a copy of the License at
	http://www.mozilla.org/MPL/
	
	Software distributed under the License is distributed on an "AS IS"
	basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the
	License for the specific language governing rights and limitations
	under the License.
	
	The Original Code is Fever Framework code.
	
	The Initial Developer of the Original Code is Romain Ecarnot.
	Portions created by Initial Developer are Copyright (C) 2006
	the Initial Developer. All Rights Reserved.
	
	Contributor(s):
	
	Unless required by applicable law or agreed to in writing, software
	distributed under the License is distributed on an "AS IS" BASIS,
	WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
	See the License for the specific language governing permissions and
	limitations under the License.
-----------------------------------------------------------------------------*/

import com.bourre.events.BasicEvent;

import fever.app.context.abstract.ContextContainer;
import fever.app.context.abstract.Dialog;
import fever.app.context.abstract.FeverContext;
import fever.app.context.abstract.FileSystem;
import fever.app.context.abstract.IContextFactory;
import fever.app.context.browser.BrowserContainer;
import fever.app.context.browser.BrowserDialog;
import fever.app.context.browser.HTTPFilesystem;
import fever.app.context.browser.SOFilesystem;
import fever.app.local.Localisation;
import fever.data.hasher.MD5;
import fever.events.FeverController;
import fever.events.FeverEventList;
import fever.Fever;
import fever.log.FeverDebug;

import flash.external.ExternalInterface;

/**
 * Builds a <strong>Browser context</strong> for application.
 * 
 * @see fever.app.context.ContextManager
 * 
 * @author Romain Ecarnot
 */
class fever.app.context.browser.BrowserContext extends FeverContext implements IContextFactory
{
	//-------------------------------------------------------------------------
	// Private properties
	//-------------------------------------------------------------------------
	
	private static var _instance : BrowserContext;
	
	private var _bIEBrowserType : Boolean;
	
	
	//-------------------------------------------------------------------------
	// Public Properties
	//-------------------------------------------------------------------------	
	
	/** {@code true} to enable HTTPFileSystem, either use SOFileSystem. */
	public static var remoteFileSystem : Boolean = false;
	
	
	//-------------------------------------------------------------------------
	// Public API
	//-------------------------------------------------------------------------
	
	/**
	 * Returns {@link fever.app.context.ContextManager} instance.
	 * 
	 * <p>Use {@link #init} method to define context.
	 */
	public static function getInstance( ) : BrowserContext
	{
		if( !_instance ) _instance = new BrowserContext( );
		return _instance;
	}
	
	/**
	 * Creates Browser application container.
	 * 
	 * <p>Use ExternalInterface with Javascript client side.
	 */
	public function createContainer() : ContextContainer
	{
		return new BrowserContainer();
	}
	
	/**
	 * Inits Filesystem I/O.
	 */
	public function createFileSystem() : FileSystem
	{
		if( remoteFileSystem ) return HTTPFilesystem.getInstance();
		else return SOFilesystem.getInstance();
	}
	
	/**
	 * Creates dialog system.
	 */
	public function createDialog() : Dialog
	{
		return new BrowserDialog();
	}
	
	/**
	 * Opens a external document.
	 */
	public function openDocument( path : String ) : Void
	{
		var pageName : String = MD5.getProcess().hash( path );
		getURL( path, pageName );
	}
	
	/** 
	 * Run an external application.
	 */
 	public function runProcess( app : String, args : String, workingDir : String, hidden : Boolean, waitFor : Boolean )
 	{
 		FeverDebug.WARN( 'run process is not available on web context' );
 	}
 	
	/** 
	 * Quit application.
	 */
	public function quit( force : Boolean ) : Void
	{
		if( force == true ) _exit(); 
		else
		{
			var o : FeverController = FeverController.getInstance();
			if( o.isRegistred( FeverEventList.onExitApplicationEVENT ) )
			{
				FeverController.getInstance().broadcastEvent( new BasicEvent( FeverEventList.onExitApplicationEVENT ) );
			}
			else
			{
		
				var confirm : Boolean = Fever.dialog.confirm( 
					Localisation.getResource( 'fever:common.quitMessage' ) 
				);
				if( confirm ) _exit();
			}
		}
	}
 	
 	/**
	 * Returns {@code true} if application is run under <strong>Internet 
	 * Explorer</strong> browser.
	 */
	public function isIEBrowser(  ) : Boolean
	{
		return _bIEBrowserType;
	}
	
	
	//-------------------------------------------------------------------------
	// Private implementation
	//-------------------------------------------------------------------------
	
	/**
	 * Constructor.
	 */
	private function BrowserContext()
	{
		super();
		
		if( !ExternalInterface.available )
			FeverDebug.ERROR( 'ExternalInterface is not available' );
		else
		{
			var f : String = "";
			f += "function getBrowserType( ){";
			f += "if ( navigator.plugins && navigator.mimeTypes && navigator.mimeTypes.length ) {";
			f += "return false;";
			f += "} else return true;";
			f += "}";
			
			ExternalInterface.call( 'eval', f );
			_bIEBrowserType = Boolean( ExternalInterface.call( 'getBrowserType' ) );
		}
	}
	
	private function _exit() : Void
	{
		var referer : String = String( ExternalInterface.call( 'eval', 'location.href' ) );
		var i : Number;
		
		i = referer.indexOf( '?' );
		if( i > 0 ) referer = referer.substring( 0, i );
		
		i = referer.indexOf( '#' );
		if( i > 0 ) referer = referer.substring( 0, i );
		
		FeverDebug.DEBUG( 'Exit application throw ' + referer );
		
		getURL( referer );
	}

}